﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using org.in2bits.MyXls;
using System.Data;

namespace AdminWebSite
{

    /// <summary>
    ///AdminCommon 的摘要说明
    /// </summary>
    public static class AdminCommon
    {


        /// <summary>
        /// 绑定数据库生成XLS报表
        /// </summary>
        /// <param name="ds">获取DataSet数据集</param>
        /// <param name="xlsName">报表表名</param>
        public static void xlsGridview(DataSet ds, string xlsName)
        {
            XlsDocument xls = new XlsDocument();

            int rowIndex = 1;
            int colIndex = 0;

            System.Data.DataTable table = ds.Tables[0];
            Worksheet sheet = xls.Workbook.Worksheets.AddNamed("信息");//状态栏标题名称
            Cells cells = sheet.Cells;
            foreach (DataColumn col in table.Columns)
            {
                colIndex++;
                //sheet.Cells.AddValueCell(1,colIndex,col.ColumnName);//添加XLS标题行
                Cell cell2 = cells.AddValueCell(1, colIndex, col.ColumnName);
                cell2.Font.Bold = true;//设置标题行的字体为粗体
                cell2.Font.FontFamily = FontFamilies.Roman;//设置标题行的字体为FontFamilies.Roman
            }

            foreach (DataRow row in table.Rows)
            {
                rowIndex++;
                colIndex = 0;
                foreach (DataColumn col in table.Columns)
                {
                    colIndex++;
                    //sheet.Cells.AddValueCell(rowIndex, colIndex, row[col.ColumnName].ToString());//将数据添加到xls表格里
                    Cell cell = cells.AddValueCell(rowIndex, colIndex, row[col.ColumnName].ToString());//转换为数字型
                    //如果你数据库里的数据都是数字的话 最好转换一下，不然导入到Excel里是以字符串形式显示。
                    cell.Font.FontFamily = FontFamilies.Roman; //字体
                    cell.Font.Bold = false;  //字体为粗体  
                }
            }
            xls.Send();
        }

    }

}